package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicMaterial;
import cn.stylefeng.guns.modular.basic.mapper.BasicMaterialMapper;
import cn.stylefeng.guns.modular.basic.model.params.BasicMaterialParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicMaterialResult;
import  cn.stylefeng.guns.modular.basic.service.BasicMaterialService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 物料大类表 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2019-09-17
 */
@Service
public class BasicMaterialServiceImpl extends ServiceImpl<BasicMaterialMapper, BasicMaterial> implements BasicMaterialService {

    @Override
    public void add(BasicMaterialParam param){
        BasicMaterial entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BasicMaterialParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BasicMaterialParam param){
        BasicMaterial oldEntity = getOldEntity(param);
        BasicMaterial newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BasicMaterialResult findBySpec(BasicMaterialParam param){
        return null;
    }

    @Override
    public List<BasicMaterialResult> findListBySpec(BasicMaterialParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BasicMaterialParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BasicMaterialParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BasicMaterial getOldEntity(BasicMaterialParam param) {
        return this.getById(getKey(param));
    }

    private BasicMaterial getEntity(BasicMaterialParam param) {
        BasicMaterial entity = new BasicMaterial();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
